﻿/***********************************************************
Copyright © 2015 Howyoung.
************************************************************
You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty.
************************************************************
您可以对所收受的本程序源代码，无论以何种媒介，复制与发布其完
整的复制物，然而您必须以显著及适当的方式在每一份复制物上发布
适当的著作权标示及无担保声明。
***********************************************************/
using Microsoft.Win32;
using System;
using System.Windows.Forms;

namespace UpdateProgram
{
    public partial class Uninstall : Form
    {
        public Uninstall()
        {
            InitializeComponent();
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            Welcome Welcome = new Welcome();
            Welcome.Show();
            Dispose();
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            try
            {
                bool programFound = false;
                RegistryKey uninstallKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true);
                foreach (string keyName in uninstallKey.GetSubKeyNames())
                {
                    RegistryKey appKey = uninstallKey.OpenSubKey(keyName);
                    if (Convert.ToString(appKey.GetValue("DisplayName")) == "Minecraft存档备份")
                    {
                        programFound = true;
                    }
                }
                if (programFound)
                {
                    if (MessageBox.Show("原程序似乎没有被卸载，您确定要继续吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                        return;
                }
                Setup Setup = new Setup();
                Setup.Show();
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(":(" + Environment.NewLine + "很抱歉，我们遇到了麻烦。" + Environment.NewLine + Environment.NewLine +
                "这是可能有用的信息：" + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine +
                "您可以试试：" + Environment.NewLine + "请再试一次。", "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            try
            {
                bool programFound = false;
                RegistryKey uninstallKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall", true);
                foreach (string keyName in uninstallKey.GetSubKeyNames())
                {
                    RegistryKey appKey = uninstallKey.OpenSubKey(keyName);
                    if (Convert.ToString(appKey.GetValue("DisplayName")) == "Minecraft存档备份")
                    {
                        programFound = true;
                    }
                }
                if (!programFound)
                {
                    timer.Enabled = false;
                    Setup Setup = new Setup();
                    Setup.Show();
                    Close();
                }
            }
            catch (Exception)
            { }
        }
    }
}
